package com.chub.study.modules.controller;


import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chub.RespResult;
import com.chub.study.modules.entity.TClassInfo;
import com.chub.study.modules.mapper.TClassInfoMapper;
import com.chub.study.modules.service.TClassInfoService;
import com.chub.study.util.SmartWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 班级信息表 前端控制器
 * </p>
 *
 * @author chub
 * @since 2021-08-26
 */
@Api(tags = "班级信息表服务")
@RestController
@RequestMapping("/modules/tclassinfo")
public class TClassInfoController extends BaseController {

    @Resource
    private TClassInfoMapper classInfoMapper;

    @Resource
    private TClassInfoService classInfoService;

    @ApiOperation(value = "根据班级名称获取班级信息。")
    @GetMapping("/getClassInfo")
    public List<TClassInfo> getXingzhengqu(@RequestParam @ApiParam("班级名称") String bjmc) {
        logger.info("要查询的班级名称 = {}", bjmc);
        QueryWrapper<TClassInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("bjmc", bjmc);
        List<TClassInfo> list = null;
        if (bjmc != null) {
            list = classInfoMapper.selectList(queryWrapper);
        } else {
            list = classInfoMapper.selectList(null);
        }
        return list;
    }


    @ApiOperation("创建")
    @PostMapping("/create")
    public RespResult<String> create(@Valid @RequestBody TClassInfo tClassInfo) {
        classInfoService.save(tClassInfo);
        return RespResult.success();
    }

    @ApiOperation(value = "删除")
    @PostMapping("/delete")
    public RespResult<String> delete(@RequestParam String id) {
        classInfoService.removeById(id);
        return RespResult.success();
    }

    @ApiOperation(value = "修改")
    @PostMapping("/modify")
    public RespResult<String> modify(@Valid @RequestBody TClassInfo tClassInfo) {
        classInfoService.updateById(tClassInfo);
        return RespResult.success();
    }

    @ApiOperation(value = "查询详情")
    @GetMapping("/detail")
    public RespResult<TClassInfo> detail(@RequestParam String id) {
        return RespResult.success(classInfoService.getById(id));
    }

    @ApiOperation(value = "分页查询")
    @PostMapping("/queryByPage")
    public RespResult<IPage<TClassInfo>> queryByPage(@RequestBody JSONObject queryObject) {
        logger.info("queryObject = {}", queryObject.toString());
        SmartWrapper<TClassInfo> smartWrapper = new SmartWrapper<>();
        smartWrapper.parse(queryObject, TClassInfo.class);

        QueryWrapper<TClassInfo> queryWrapper = smartWrapper.getQueryWrapper();
        queryWrapper.eq("BJBH", queryObject.get("BJBH"));
        queryWrapper.likeRight("BJMC", queryObject.get("BJMC"));

        IPage<TClassInfo> page = smartWrapper.generatePage();
        classInfoService.page(page, queryWrapper);
        return RespResult.success(page);
    }

}

